# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/28 上午9:26
 @Author  : tgy

 https://leetcode-cn.com/problems/find-pivot-index/
"""

class Solution:

    def pivotIndex(self, nums: list) -> int:
        """
        总和为 all,i 位置为中心点，中心点的左侧为 left_sum ,则右边为 all - nums[i] - left_sum
        要为中心点，左右两边相等:
            left_sum = all - nums[i] - left_sum
            all = 2*left_sum + nums[i]
        """
        if not nums:
            return -1;
        all = sum(nums)
        left_sum = 0
        for i in range(len(nums)):
            ...
            if ((left_sum << 1) + nums[i]) == all:
                return i
            else:
                left_sum += nums[i]

        return -1

    def pivotIndex01(self, nums: list) -> int:
        ...
        if not nums:
            return -1
        nums_len = len(nums)
        left_sum = 0
        right_sum = 0
        for i in range(1,nums_len):
            right_sum += nums[i]

        for i in range(0, nums_len):
            ...
            if left_sum == right_sum:
                return i
            else:
                left_sum += nums[i]
                right_sum -= nums[i+1]


        return -1

if __name__ == '__main__':
    ...
